#include <iostream> // cout
#include <cmath> // sin cos
#include <GL/glut.h>
#include <GL/gl.h>
//==========================================
// globales

extern bool wire,relleno;
extern int plano,lod;
extern GLuint texid;
extern bool mipmap_ppm(char *ifile);;

//==========================================

void drawWiredCube() {
	glColor3f(1,1,1);
	glBegin(GL_LINES);
	glVertex3f(-1,-1,-1); glVertex3f(1,-1,-1);
	glVertex3f(-1,-1,-1); glVertex3f(-1,1,-1);
	glVertex3f(-1,-1,-1); glVertex3f(-1,-1,1);
	glVertex3f(1,1,1); glVertex3f(1,1,-1);
	glVertex3f(1,1,1); glVertex3f(1,-1,1);
	glVertex3f(1,1,1); glVertex3f(-1,1,1);
	glVertex3f(-1,-1,1); glVertex3f(1,-1,1);
	glVertex3f(-1,-1,1); glVertex3f(-1,1,1);
	glVertex3f(-1,1,-1); glVertex3f(-1,1,1);
	glVertex3f(-1,1,-1); glVertex3f(1,1,-1);
	glVertex3f(1,-1,-1); glVertex3f(1,1,-1);
	glVertex3f(1,-1,-1); glVertex3f(1,-1,1);
	glEnd();
}

void drawSphere(double radio, GLfloat* ball_color){
glPushAttrib(GL_ALL_ATTRIB_BITS);
	glPushMatrix();
	GLint slices=lod, stacks=lod;
	
	if (relleno){
		glColor4fv(ball_color);
		glEnable(GL_CULL_FACE);
		glutSolidSphere(radio,slices,stacks); 
	}
	glPopAttrib();
	glPopMatrix();
}

void drawMesa(){
	glColor3f(0.1,.4,.7);
	glBegin(GL_QUADS);
	//plano hacia -x
	glNormal3f(-1,0,0);
	glVertex3f(-1,-1,1);
	glVertex3f(-1,1,1);
	glVertex3f(-1,1,-1);
	glVertex3f(-1,-1,-1);
	//plano hacia -z
	glNormal3f(0,0,-1);
	glVertex3f(-1,1,-1);
   	glVertex3f(-1,-1,-1);
   	glVertex3f(1,-1,-1);
   	glVertex3f(1,1,-1);
	
	//plano hacia y
	
	glNormal3f(0,1,0);
	
	
	glTexCoord2d(1,0);glVertex3f(-1,1,1);
	glTexCoord2d(1,1);glVertex3f(1,1,1);
	glTexCoord2d(0,1);glVertex3f(1,1,-1);
	glTexCoord2d(0,0);glVertex3f(-1,1,-1);
	glDisable(GL_TEXTURE_2D);
	

	//plano hacia -y
	glNormal3f(0,-1,0);
	glVertex3f(-1,-1,1);
	glVertex3f(1,-1,1);
	glVertex3f(1,-1,-1);
	glVertex3f(-1,-1,-1);
	//plano hacia x
	glNormal3f(1,0,0);
	glVertex3f(1,-1,1);
	glVertex3f(1,1,1);
	glVertex3f(1,1,-1);
	glVertex3f(1,-1,-1);
	//plano hacia z
	glNormal3f(0,0,1);	
	glVertex3f(-1,-1,1);
	glVertex3f(1,-1,1);
	glVertex3f(1,1,1);
	glVertex3f(-1,1,1);
	glEnd();
}
void drawPrisma(GLfloat* ball_color){
	glColor4fv(ball_color);
	glBegin(GL_QUADS);
	glNormal3f(-1,0,0);
	
	glVertex3f(-1,-1,1);
	glVertex3f(-1,1,1);
	glVertex3f(-1,1,-1);
	glVertex3f(-1,-1,-1);
	
	glNormal3f(0,0,-1);
	glVertex3f(-1,1,-1);
    glVertex3f(-1,-1,-1);
    glVertex3f(1,-1,-1);
    glVertex3f(1,1,-1);
	
	glNormal3f(0,1,0);
	glVertex3f(-1,1,1);
	glVertex3f(1,1,1);
	glVertex3f(1,1,-1);
	glVertex3f(-1,1,-1);
	
	glNormal3f(0,-1,0);
	glVertex3f(-1,-1,1);
	glVertex3f(1,-1,1);
	glVertex3f(1,-1,-1);
	glVertex3f(-1,-1,-1);
	
	glNormal3f(1,0,0);
	glVertex3f(1,-1,1);
	glVertex3f(1,1,1);
	glVertex3f(1,1,-1);
	glVertex3f(1,-1,-1);
	
	glNormal3f(0,0,1);	
	glVertex3f(-1,-1,1);
	glVertex3f(1,-1,1);
	glVertex3f(1,1,1);
	glVertex3f(-1,1,1);
	glEnd();
}


GLfloat ball_color[4];

void drawObjects() {
	
	glPushMatrix();
	drawWiredCube();
	
	glPushMatrix();
	glScalef(0.5,0.02,1.0);
	drawMesa();
	glPopMatrix();
      
  ball_color[0]=.7843;
	ball_color[1]=.6901;
	ball_color[2]=0.0705;
	ball_color[3]=1;
	
	glPushMatrix();
	glTranslatef(0,0,.5);
	
	glPushMatrix();
	glTranslatef(0,.05,0);
	glScalef(.03,.03,.03);
	drawSphere(1,ball_color);
	glPopMatrix();
	
	//las 2 bolas que van en la 2da lÃ­nea
	glPushMatrix();
	glTranslatef(0.032,0.05,0.055);
	ball_color[0]=0.4627;
	ball_color[1]=0;
	ball_color[2]=0;
	glScalef(.03,.03,.03);
	drawSphere(1,ball_color);
	glPopMatrix();

	glPushMatrix();
	glTranslatef(-0.032,0.05,0.055);
	ball_color[0]=.0470;
	ball_color[1]=.04331;
	ball_color[2]=.2705;
	glScalef(.03,.03,.03);
	drawSphere(1,ball_color);
	glPopMatrix();
	
	//las 3 bolas en la 3ra lÃ­nea
	glPushMatrix();
	glTranslatef(0,0.05,0.11);
	ball_color[1]=0;
	ball_color[0]=0;
	ball_color[2]=0;
	glScalef(.03,.03,.03);
	drawSphere(1,ball_color);
	glPopMatrix();
	
	glPushMatrix();
	glTranslatef(0.065,0.05,0.11);
	ball_color[0]=0.89;
	ball_color[1]=0.29;
	ball_color[2]=0.06;
	glScalef(.03,.03,.03);
	drawSphere(1,ball_color);
	glPopMatrix();
	
	glPushMatrix();
	glTranslatef(-0.065,0.05,0.11);
	ball_color[0]=0;
	ball_color[1]=.3137;
	ball_color[2]=.0549;
	glScalef(.03,.03,.03);
	drawSphere(1,ball_color);
	glPopMatrix();
	
	//las ultimas 4 bolas
	
	glPushMatrix();
	glTranslatef(0.032,0.05,0.165);
	ball_color[0]=0.31;
	ball_color[1]=0.41;
	ball_color[2]=0.98;
	glScalef(.03,.03,.03);
	drawSphere(1,ball_color);
	glPopMatrix();
	
	glPushMatrix();
	glTranslatef(-0.032,0.05,0.165);
	ball_color[0]=0.31;
	ball_color[1]=0.41;
	ball_color[2]=0.98;
	glScalef(.03,.03,.03);
	drawSphere(1,ball_color);
	glPopMatrix();
	
	
	// y si el negro me pide una bola de este color, le damos el gusto 
	glPushMatrix();
	glTranslatef(-0.095,0.05,0.165);
	ball_color[0]=.6431;
	ball_color[1]=.0941;
	ball_color[2]=.3803;
	glScalef(.03,.03,.03);
	drawSphere(1,ball_color);
	glPopMatrix();
	
	glPushMatrix();
	glTranslatef(0.095,0.05,0.165);
	ball_color[0]=0.31;
	ball_color[1]=0.41;
	ball_color[2]=0.98;
	glScalef(.03,.03,.03);
	drawSphere(1,ball_color);
	glPopMatrix();

  glPopMatrix();
	
	//las 4 patas
	
	ball_color[0]=0.0;
	ball_color[1]=0.0;
	ball_color[2]=0.0;
	glPushMatrix();
	glTranslatef(-.4,-.41,.9);
	glScalef(.06,.4,.06);
	drawPrisma(ball_color);
	glTranslatef(13,0,0);
	drawPrisma(ball_color);
	glTranslatef(0,0,-29);
	drawPrisma(ball_color);
	glTranslatef(-13,0,0);
	drawPrisma(ball_color);
	glPopMatrix();
	
	//bordes de mesa

	glPushMatrix();
	//dibujo primer borde superior izquierdo
	glTranslatef(0.47,0.02,0);
	glTranslatef(0,0,-0.45);
	glScalef(0.06/1.2,0.035/1.2,0.415/1.1);
	drawPrisma(ball_color);		
		
	glPushMatrix();
	//dibujo segundo borde, inferior izquierdo	
	glTranslatef(0.0,0.0,2.4);
	drawPrisma(ball_color);
	
	//dibujo tercer borde, inferior derecho		
	glTranslatef(-19,0,0);
	drawPrisma(ball_color);
	//con este pop vuelvo, a las cordenadas del primer borde, para poder desplazarlo hacia la izquierda
	glPopMatrix();	
	glTranslatef(-18.8,0,0);
	drawPrisma(ball_color);
	
	glPopMatrix();
	//borde superior
	glPushMatrix();
	glTranslatef(0,0,-1);
	glScalef(0.415/1.1,0.035/1.2,0.06/1.2);
	drawPrisma(ball_color);
	//borde inferior
	glTranslatef(0,0,40);
	drawPrisma(ball_color);	
	glPopMatrix();
	
	//dibujo la caja de la mesa
	
	glPushMatrix();
	glTranslatef(0,-.1,.9);
	glScalef(.4,.1,0.05);
	drawPrisma(ball_color);
	glPopMatrix();
	
	glPushMatrix();
	
	glTranslatef(0,-.1,-.845);
	glScalef(.4,.1,0.05);
	drawPrisma(ball_color);
	glPopMatrix();
	
		
	glPushMatrix();
	
	glTranslatef(0.4,-.1,.03);
	glScalef(.04,.1,.81);
	drawPrisma(ball_color);
	glPopMatrix();
	
	glPushMatrix();
	glTranslatef(-0.4,-.1,.03);
	glScalef(.04,.1,.81);
	drawPrisma(ball_color);
	glPopMatrix();
	
	glPopMatrix();
	
}

